
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:查询游币余额
 *
 * @author wcj
 * @since 2015年11月17日 下午3:04:45
 */
public class QueryKubiNumTask {

    /**
     * 获取游币余额请求接口
     * 
     * @param userName
     * @param kgToken kugouToken
     * @return
     */
    public QueryKubiNumHttpResponse request(String userName, String kgToken) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        // 填充参数，key-value。key是接口要求传的变量名称
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("cmd", ProtocolCmd.CMD_QUERY_YOUBI);
        params.put("username", userName);
        params.put("kgtoken", kgToken);

        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        requestPackage.setUserName(userName);
        requestPackage.setKgToken(kgToken);
        // 生成响应包
        ResponsePackage responsePackage = new HttpResponsePackage();
        // 生成响应类
        QueryKubiNumHttpResponse appsHttpResponse = new QueryKubiNumHttpResponse();

        requestParams.add(params);
        requestPackage.setRequestParams(requestParams);

        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    private class HttpResponsePackage extends HttpBaseResponsePackage<QueryKubiNumHttpResponse> {

        @Override
        protected void handleResponse(QueryKubiNumHttpResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "QueryKubiNumTask:" + jsonStr);
                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (response.isUseful()) {
                        JSONObject jsonResult = jsonResp.getJSONObject("data");
                        response.setResult_kbleft(jsonResult.optInt("kbleft"));
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
            }
        }
    }

    public class QueryKubiNumHttpResponse extends ResponseMessage {

        /**
         * 
         */
        private static final long serialVersionUID = 4259071806346641563L;

        private int result_kbleft;

        public int getResult_kbleft() {
            return result_kbleft;
        }

        public void setResult_kbleft(int result_kbleft) {
            this.result_kbleft = result_kbleft;
        }

    }
}
